package com.sc.nft.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.sc.nft.dao.DigitalCollectionProductInfoSubscribeCodeDao;
import com.sc.nft.dao.DigitalCollectionProductInfoSubscribeDao;
import com.sc.nft.entity.DigitalCollectionProductInfoSubscribe;
import com.sc.nft.entity.DigitalCollectionProductInfoSubscribeCode;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.vo.SubscribeProductDetailVo;
import com.sc.nft.entity.vo.SubscribeProductListVo;
import com.sc.nft.enums.ProductInfoSubscribeScheduleEnum;
import com.sc.nft.service.SubscribeProductService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;

/**
*@author 刘国航
*@since 2024/5/16 17:58
*/
@Slf4j
@RequiredArgsConstructor
@Service
public class SubscribeProductServiceImpl implements SubscribeProductService {

    private final DigitalCollectionProductInfoSubscribeCodeDao digitalCollectionProductInfoSubscribeCodeDao;
    private final DigitalCollectionProductInfoSubscribeDao digitalCollectionProductInfoSubscribeDao;

    @Override
    public Page<SubscribeProductListVo> list(UserInfo userInfo, Page page) {
        return digitalCollectionProductInfoSubscribeCodeDao.getByProductIdLeft(userInfo.getId(),page);
    }

    @Override
    public Page<SubscribeProductDetailVo> detail(UserInfo userInfo, Long productId, Page page) {
        DigitalCollectionProductInfoSubscribe digitalCollectionProductInfoSubscribe = digitalCollectionProductInfoSubscribeDao.getByProductInfoSubscribe(productId);
        if(Objects.isNull(digitalCollectionProductInfoSubscribe)) {
            return new Page<>();
        }
        DateTime now = DateTime.now();
        Page<DigitalCollectionProductInfoSubscribeCode> digitalCollectionProductInfoSubscribeCodePage = digitalCollectionProductInfoSubscribeCodeDao.getPageByUserProductIdAndSchedule(page, userInfo.getId(), productId, null, now.isAfter(digitalCollectionProductInfoSubscribe.getConsistenceEndTime()));
        Page<SubscribeProductDetailVo> mySubscribeCodesDataVoPage = new Page<>();
        mySubscribeCodesDataVoPage.setCurrent(digitalCollectionProductInfoSubscribeCodePage.getCurrent());
        mySubscribeCodesDataVoPage.setPages(digitalCollectionProductInfoSubscribeCodePage.getPages());
        mySubscribeCodesDataVoPage.setTotal(digitalCollectionProductInfoSubscribeCodePage.getTotal());
        mySubscribeCodesDataVoPage.setSize(digitalCollectionProductInfoSubscribeCodePage.getSize());
        List<DigitalCollectionProductInfoSubscribeCode> records = digitalCollectionProductInfoSubscribeCodePage.getRecords();
        List<SubscribeProductDetailVo> mySubscribeCodesDataVoList = Lists.newArrayList();
        if(Objects.nonNull(records) && !records.isEmpty()) {
            records.forEach(record -> {
                SubscribeProductDetailVo mySubscribeCodesDataVo = new SubscribeProductDetailVo();
                mySubscribeCodesDataVo.setCreateTime(record.getCreateTime());
                //region 抽签活动结束之前，不展示抽签结果
                if(now.isBefore(digitalCollectionProductInfoSubscribe.getConsistenceStartTime())) {
                    mySubscribeCodesDataVo.setWinStatus(ProductInfoSubscribeScheduleEnum.WAIT_FOR_WIN);
                } else if(now.isBefore(digitalCollectionProductInfoSubscribe.getConsistenceEndTime())) {
                    mySubscribeCodesDataVo.setWinStatus(ProductInfoSubscribeScheduleEnum.WINING);
                } else {
                    mySubscribeCodesDataVo.setWinStatus(record.getSchedule());
                }
                //endregion
                mySubscribeCodesDataVo.setSubscribeCode(record.getSubscribeCode());
                mySubscribeCodesDataVoList.add(mySubscribeCodesDataVo);
            });
        }
        mySubscribeCodesDataVoPage.setRecords(mySubscribeCodesDataVoList);
        return mySubscribeCodesDataVoPage;
    }

}
